package org.example.strategypatterndemo.Strategy;

import lombok.Data;
import org.example.strategypatterndemo.module1.service.PromotionStrategy;
import org.springframework.stereotype.Component;

/**
 * OrderContext
 * @author dingchen
 * @version 2025/05/29 19:31
**/
@Component
public class OrderContext {
    // 当前使用的策略（默认是空的）
    private PromotionStrategy currentStrategy;

    // 无参构造函数
    public OrderContext() {
    }

    // 有参构造函数
    public OrderContext(PromotionStrategy strategy) {
        this.currentStrategy = strategy;
    }

    // 随时换策略！就像吃饭时突然想换菜，告诉服务员就行
    public void changeStrategy(PromotionStrategy newStrategy) {
        this.currentStrategy = newStrategy;
    }

    // 计算最终价格：交给当前策略去处理
    public double calculateFinalPrice(double orderAmount) {
        if (currentStrategy == null) {
            throw new IllegalStateException("No promotion strategy has been set.");
        }
        return orderAmount - currentStrategy.calculateDiscount(orderAmount);
    }
}
